package com.example.myword.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    /*jwt令牌生成与验证类*/
    private static final String KEY = "kirito";//密钥
    private static final long EXPIRE_TIME = 1000 * 60 * 60;

    /**处理业务数据，生成token并返回*/
    public static String genToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)//添加载荷
//                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                .sign(Algorithm.HMAC256(KEY));
    }

    /**接收token，验证token并返回业务数据*/
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims") //获取载荷中的信息
                .asMap();
    }
}
